package com.token.utils;

/**
 * @auther: henriy
 * @Date: 2023/11/21
 */
public class DistanceCalculator {
    private static final double EARTH_RADIUS = 6371; // 地球半径，单位千米

    public static double calculateDistance(double lat1, double lon1, double lat2, double lon2) {
        // 将经纬度转换为弧度
        double radLat1 = Math.toRadians(lat1);
        double radLon1 = Math.toRadians(lon1);
        double radLat2 = Math.toRadians(lat2);
        double radLon2 = Math.toRadians(lon2);

        // 计算经纬度之间的差距
        double deltaLat = radLat2 - radLat1;
        double deltaLon = radLon2 - radLon1;

        // 应用Haversine公式计算距离
        double a = Math.pow(Math.sin(deltaLat / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(deltaLon / 2), 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        double distance = EARTH_RADIUS * c;

        return distance;
    }

    public static void main(String[] args) {
        double lon1 = 108.92318; // 经度1
        double lat1 = 34.25180; // 纬度1
        double lon2 = 108.93311; // 经度2
        double lat2 = 34.27990; // 纬度2

        double distance = calculateDistance(lat1, lon1, lat2, lon2);

        double a = 3.255100144943721;
        double b = 3.1463480382298563;
        double c = 6.401448135008418;

        double result = (a + b + c) / 2 - c;
        System.out.println(result > 0);
        System.out.println(result);
        System.out.println("两个经纬度之间的距离为：" + distance + "千米");
    }

}

